﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace RedRiver.Framework.Web.Models{
    public static class ProcessContextExtension{
        /// <summary>
        /// 根据用户名分析获取 工号
        /// </summary>
        /// <param name="processContext"></param>
        /// <returns></returns>
        public static string GetEmployeeCode(this ProcessContext processContext){
            if (processContext == null){
                throw new ArgumentNullException("processContext");
            }
            if (string.IsNullOrEmpty(processContext.EmployeeCode)){
                return string.Empty;
            }
            string empCode = processContext.EmployeeCode;
            if (processContext.EmployeeCode.StartsWith("EP", StringComparison.CurrentCultureIgnoreCase)){
                return processContext.EmployeeCode.Substring(2);
            }

            if ((empCode.Count(c=>!Char.IsDigit(c))==0) &&(empCode.Length < 6) && (empCode.Length > 0)){
                int empNum = 0;
                int.TryParse(processContext.EmployeeCode, out empNum);
                return empNum.ToString("000000");
            }
            using (var employees = new  EmployeesDataContext()){
                var employee = employees.Employee.FirstOrDefault(e => e.UserName == empCode);
                if(employee!=null){
                    return employee.EmployeeCode;
                }
            }
            return empCode;
        }
    }

    /// <summary>
    /// 架构中不存在 WorkTeamEntities 的注释。
    /// </summary>
    [MetadataType(typeof (IProcessContextMetadata))]
    partial class ProcessContext{
    }

    public interface IProcessContextMetadata{
        [DisplayName("姓名")]
        string FullName { get; }

        [DisplayName("部门")]
        string DepName { get; }

        [DisplayName("车间")]
        string Workshop { get; }

        [DisplayName("工号")]
        string EmployeeCode { get; set; }

        [DisplayName("班组")]
        string WorkTeam { get; set; }

        [DisplayName("生产线")]
        string WorkLine { get; set; }

        [DisplayName("工序")]
        string ProcId { get; set; }

        [DisplayName("工位")]
        string WorkStation { get; set; }

        [DisplayName("默认操作")]
        string DefaultAction { get; set; }

        [DisplayName("描述")]
        string Description { get; set; }
    }
}